package com.ruoyi.web.controller.price;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.ICategoryService;
import com.ruoyi.service.IRegionService;
import com.ruoyi.web.domain.Category;
import com.ruoyi.web.domain.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/price/category")
public class CategoryController extends BaseController {
    private String prefix = "price/category";

    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IRegionService regionService;

    @GetMapping("/list")
    public String list(Category po, ModelMap mmap) {
//        startPage();
        List<Category> list = categoryService.selectCategoryList(po);
//        TableDataInfo table = getDataTable(list);
        mmap.put("categorys", list);
        return prefix + "/list";
    }

    /**
     * 修改角色
     */
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Integer categoryId, ModelMap mmap){
        mmap.put("category", categoryService.selectCategoryById(categoryId));
        return prefix + "/edit";
    }

    @GetMapping("/delete/{vcrId}")
    public String delete(@PathVariable("vcrId") Integer vcrId, ModelMap mmap){
        mmap.put("category", categoryService.deleteRelation(vcrId));
        return redirect("/price/category/list");
    }

    /**
     * 修改保存角色
     */
    @PostMapping("/edit")
    public String editSave(@Validated Category role) {
        categoryService.updateCategory(role);
        return redirect("/price/category/list");
    }

    @GetMapping("/toSave")
    public String toSave(ModelMap mmap){
        List<Region> regions = regionService.selectRegionList(new Region());
        mmap.put("regions", regions);
        return prefix + "/add";
    }

    @PostMapping("/save")
//    @ResponseBody
    public String saveCategory(@Validated Category role) {
         categoryService.saveCategory(role);
        return redirect("/price/category/list");
    }
}
